package com.smy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smy.reggie.common.BaseContext;
import com.smy.reggie.common.CustomException;
import com.smy.reggie.common.R;
import com.smy.reggie.pojo.ShoppingCart;
import com.smy.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController  {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户Id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //条件查询
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //当前用户下的购物车
        wrapper.eq(ShoppingCart::getUserId,currentId);
        //当前添加是菜品/套餐
        if(shoppingCart.getDishId() != null){//菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {//套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(wrapper);
        //当前菜品/套餐是否已经存在
        if(shoppingCart1 != null){//已经存在
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCart1);
        }else {
            //不存在
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shoppingCart1 = shoppingCart;
        }
        return R.success(shoppingCart1);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //获取当前用户Id
        Long currentId = BaseContext.getCurrentId();
        //条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //当前用户下的购物车
        wrapper.eq(ShoppingCart::getUserId,currentId);
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        //获取当前用户Id
        Long currentId = BaseContext.getCurrentId();
        //条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //当前用户下的购物车
        wrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功!");
    }
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户Id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //条件查询
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //当前用户下的购物车
        wrapper.eq(ShoppingCart::getUserId,currentId);
        //当前添加是菜品/套餐
        if(shoppingCart.getDishId() != null){//菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {//套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(wrapper);
        //当前菜品/套餐是否已经存在
        if(shoppingCart1 != null){//已经存在
            Integer number = shoppingCart1.getNumber();
            if(number == 1){
                shoppingCartService.remove(wrapper);
            }
            shoppingCart1.setNumber(number - 1);
            shoppingCartService.updateById(shoppingCart1);
            return R.success("修改成功！");
        }else {
            //不存在
            throw new CustomException("该对象不存在!");
        }

    }

}
